package com.anhuanjia.tools.eao.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoAudit;
import com.anhuanjia.tools.eao.bean.EaoAuditReply;
import com.anhuanjia.tools.eao.bean.EaoProjectDoc;
import com.anhuanjia.tools.eao.bean.EaoSubmitTime;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.i18n.Lang;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 审核内容操作
 * 
 * @author Administrator
 *
 */
@Alias(value = "auditreply")
public class AuditReplyUI extends UIFacade {

	/**
	 * 添加/更改内容表
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String s = "审核开始";
		if (!s.equals("审核开始")) {
			f("审核未开始，不可更改");
			return;
		}
		Transaction transaction = new Transaction();
		EaoAuditReply entity = new EaoAuditReply();
		entity.setValue(this.Request);
		String id = entity.getId();
		if (StringUtils.isEmpty(id)) {
			entity.setId(UUID.randomUUID().toString());
			DataTable dta = new Q("select * from eao_audit where id = ?", entity.getAuditId()).fetch();
			EaoAudit main = new EaoAudit();
			if (dta.getRowCount() != 0) {
				main.setValue(dta.get(0));
				if (main.getStats().equals("已审核")) {
					f("审核已关闭，不可更改");
					return;
				}
				transaction.insert(entity);
				if (transaction.commit()) {
					s(Lang.get("Common.ExecuteSuccess"));
				} else {
					f(Lang.get("Common.ExecuteFailed"));
				}
			}
		} else {
			DataTable dt = new Q(" select * from eao_audit_reply where id = ?", id).fetch();
			EaoAuditReply old = new EaoAuditReply();
			old.setValue(dt.get(0));
			if (StringUtils.isEmpty(old.getAuditId())) {
				f("操作失败，请确认信息是否存在");
				return;
			}
			DataTable dta = new Q("select * from eao_audit where id = ?", old.getAuditId()).fetch();
			EaoAudit main = new EaoAudit();
			main.setValue(dta.get(0));
			if (main.getStats().equals("已审核")) {
				f("审核已关闭，不可更改");
				return;
			}
			old.setValue(this.Request);
			transaction.update(old);
			if (transaction.commit()) {
				s(Lang.get("Common.ExecuteSuccess"));
			} else {
				f(Lang.get("Common.ExecuteFailed"));
			}
		}
	}

	/**
	 * 查询 查询单条记录
	 */
	@Priv(loginType = LoginType.Member)
	public void findOne() {
		String searchKey = $V("SearchKey");
		if (StringUtils.isEmpty(searchKey)) {
			f("请输入搜索条件！");
			return;
		}
		Q q = new Q("select * from eao_audit_reply where id = ? ", searchKey);
		$S("data", q.fetch());
	}

	/**
	 * 查询所有
	 */
	@Priv(loginType = LoginType.Member)
	public void findAll() {
		DataTable dt = new Q("select * from eao_audit_reply ").fetch();
		List<EaoAuditReply> list = new ArrayList<>();
		for (DataRow dr : dt) {
			EaoAuditReply entity = new EaoAuditReply();
			entity.setValue(dr);
			list.add(entity);
			this.Response.put("data", list);
		}
	}

	/**
	 * 根据id删除
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteByid() {
		String id = $V("Id");
		if (!StringUtil.isNotEmpty(id)) {
			f("操作失败，请确认操作信息");
			return;
		}
		DataTable dt = new Q(" select * from eao_audit_reply where id = ?", id).fetch();
		EaoAuditReply old = new EaoAuditReply();
		old.setValue(dt.get(0));
		if (StringUtils.isEmpty(old.getAuditId())) {
			f("操作失败，请确认信息是否存在");
			return;
		}
		DataTable dta = new Q("select * from eao_audit where id = ?", old.getAuditId()).fetch();
		EaoAudit main = new EaoAudit();
		main.setValue(dta.get(0));
		if (main.getStats().equals("已审核")) {
			f("审核已关闭，不可更改");
			return;
		}
		if (main.getStats().equals("未提交")) {
			f("操作人未提交审核，不可更改");
			return;
		}
		Transaction trans = new Transaction();
		Q qTwo = new Q(" delete from eao_audit_reply  where ").in("id", id);
		trans.add(qTwo);
		if (trans.commit()) {
			s(Lang.get("Common.ExecuteSuccess"));
		} else {
			f(Lang.get("Common.ExecuteFailed"));
		}
	}

	/**
	 * 多条修改、添加
	 * 
	 * @param AuditReplyLists
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdateM() {
		String AuditReplyLists = $V("AuditReplyLists");
		if(StringUtils.isEmpty(AuditReplyLists)) {
			f("参数传入失败");
		}
		Transaction transaction = new Transaction();
		List<Map<String, Object>> array = JSON.parseObject(AuditReplyLists,
				new TypeReference<List<Map<String, Object>>>() {
				});
		String pid = $V("ProjectId");
		String atype = $V("Atype");
		String btype = "";
		String aOrc = $V("Aoc"); // 判断是审核2还是修改3
		String save = $V("Save");
		Date data = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dates = sd.format(data);
		if (StringUtils.isEmpty(pid) || StringUtils.isEmpty(atype) || StringUtils.isEmpty(aOrc)) {
			f("请设置项目id、审核阶段");
			return;
		}
		Boolean bl = true;
		for (Map<String, Object> list : array) {

			EaoAuditReply auditReply = new EaoAuditReply();
			auditReply.setValue(list);

			String id = auditReply.getId();
			if (StringUtils.isEmpty(id)) {
				// 添加
				auditReply.setId(UUID.randomUUID().toString());
				if (StringUtil.isNotEmpty(save)) {
					auditReply.setStatus("2");
				} else {
					auditReply.setStatus("1");
				}
				transaction.insert(auditReply);
//				if (transaction.commit()) {
//					s("添加成功");
//				} else {
//					f("添加失败");
//				}
			} else {
				// 更新
				DataTable dt = new Q("select * from eao_audit_reply where id = ? ", id).fetch();
				if (!"3".equals(dt.get(0, "status"))) {
					EaoAuditReply entity = new EaoAuditReply();
					entity.setValue(dt.get(0));
					entity.setValue(list);
					if ("3".equals(aOrc)) {
						if (StringUtil.isNotEmpty(save)) {
							entity.setStatus("3");
						} else {
							entity.setStatus("1");
						}
					} else if ("2".equals(aOrc)) {
						if (StringUtil.isNotEmpty(save)) {
							entity.setStatus("2");
						} else {
							entity.setStatus("1");
						}
					}
					transaction.update(entity);
					if (StringUtils.isEmpty(entity.getReviseExplain())) {
						bl = false;
					}
				}
			}
		}
		if ("0".equals(atype)) {
			atype = "10";
			btype = "一审";
		} else if ("1".equals(atype)) {
			atype = "11";
			btype = "二审";
		} else if ("2".equals(atype)) {
			atype = "12";
			btype = "初审";
		} else if ("3".equals(atype)) {
			atype = "13";
			btype = "复审";
		} else if ("4".equals(atype)) {
			atype = "14";
			btype = "终审";
		} else {
			f("审核状态无法识别！");
			return;
		}

		EaoSubmitTime submitTime = new EaoSubmitTime();
		submitTime.setId(UUID.randomUUID().toString());
		submitTime.setProjectId(pid);
		submitTime.setProjectType(atype);
		submitTime.setSubmitType(aOrc);
		DataTable dta = new Q("select * from eao_audit where projectId = ? and auditType = ?", pid, btype).fetch();
		EaoAudit exentity = new EaoAudit();
		exentity.setValue(dta.get(0));
		if ("2".equals(aOrc)) {
			exentity.setAuditDotime(dates);
			transaction.update(exentity);
			submitTime.setDataTime(btype + dates);
			DataTable dataTable = new Q(" select * from eao_project_doc where ProjectId = ? ", pid).fetch();
			if (StringUtil.isNotEmpty(save)) {
				transaction.insert(submitTime);
				if (dataTable.getRowCount() != 0) {
					EaoProjectDoc doc = new EaoProjectDoc();
					doc.setValue(dataTable.get(0));
					doc.setProjectEnvOverview("");
					transaction.update(doc);
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else if ("3".equals(aOrc)) {
			exentity.setModifierDotime(dates);
			transaction.update(exentity);
			submitTime.setDataTime("修改完成" + dates);
			DataTable dataTable = new Q(" select * from eao_project_doc where ProjectId = ? ", pid).fetch();
			if (StringUtil.isNotEmpty(save)) {
				transaction.insert(submitTime);
				if (dataTable.getRowCount() != 0) {
					EaoProjectDoc doc = new EaoProjectDoc();
					doc.setValue(dataTable.get(0));
					doc.setProjectEnvOverview("");
					transaction.update(doc);
				}
			}
			if (bl) {
				if (transaction.commit()) {
					s("更新成功");
				} else {
					f("更新失败");
				}
			} else {
				f("修改说明不能为空");
				return;
			}
		}
	}

}
